<?php
class ModelCatalogBanner extends Model {
	public function addBanner($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "banner SET title = '" . $this->db->escape($data['title']) . "', link = '" . $this->db->escape($data['link']) . "', text = '" . $this->db->escape(strip_tags($data['text'])) . "', note = '" . $this->db->escape(strip_tags($data['note'])) . "', status = '" . (int)$data['status'] . "', date_added = NOW()");

    if (isset($_POST['image'])) {
      $query = $this->db->query("SELECT banner_id FROM banner ORDER BY banner_id DESC LIMIT 1");
      $data = $query->row;
      $review_id = $data['banner_id'];
			$this->db->query("UPDATE " . DB_PREFIX . "banner SET image = '" . $_POST['image'] . "' WHERE banner_id = '" . (int)$review_id . "'");
		}
	}
	
	public function editBanner($review_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "banner SET title = '" . $this->db->escape($data['title']) . "', link = '" . $this->db->escape($data['link']) . "', text = '" . $this->db->escape(strip_tags($data['text'])) . "', note = '" . $this->db->escape(strip_tags($data['note'])) . "', status = '" . (int)$data['status'] . "', date_modified = NOW() WHERE banner_id = '" . (int)$review_id . "'");

    if (isset($_POST['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "banner SET image = '" . $_POST['image'] . "' WHERE banner_id = '" . (int)$review_id . "'");
		}
	}
	
	public function deleteBanner($review_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "banner WHERE banner_id = '" . (int)$review_id . "'");
	}
	
	public function getBanner($review_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "banner WHERE banner_id = '" . (int)$review_id . "'");
		
		return $query->row;
	}

	public function getBanners($data = array()) {
		$sql = "SELECT r.banner_id, r.link, r.note, r.title, r.status, r.date_added FROM " . DB_PREFIX . "banner r ";																																					  
		
		$sort_data = array(
		'r.note',
		'r.link',
		'r.title',
		'r.status',
		'r.date_added'
		);	
			
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY r.date_added";	
		}
			
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}																																							  
																																							  
		$query = $this->db->query($sql);																																				
		
		return $query->rows;	
	}
	
	public function getTotalBanners() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "banner");
		
		return $query->row['total'];
	}
	
	public function getTotalBannersAwaitingApproval() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "review WHERE status = '0'");
		
		return $query->row['total'];
	}	
}
?>
